package com.coderitl.springmvc.controller;

import com.coderitl.springmvc.exception.AgeException;
import com.coderitl.springmvc.exception.NameException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/exception")
public class ThrowsExceptionController {
    @RequestMapping("/exception.do")
    public ModelAndView throwsMyException(HttpSession session,String uname, Integer age) throws Exception {
        // 添加一个临时数据
        session.setAttribute("tempData","use session save temp data...");
        ModelAndView mv = new ModelAndView();
        // 抛出异常
        if (!uname.equals("coder-itl")) {
            throw new NameException("用户名不正确!");
        }
        if (age > 80) {
            throw new AgeException("您需要休息了!");
        }
        System.out.println("controller...");
        // 添加数据
        mv.addObject("uname", uname);
        mv.addObject("age", age);
        // 没有异常显示的页面
        mv.setViewName("show");
        return mv;
    }
}
